var creditRatingCtrl  = new XFApp.client();
$.views.converters({	
	
	fcreditRating : function(value) {
		if(!value){
			return "";
		}
		for(index in creditRatingStatus){
			var creditRating = creditRatingStatus[index];
			if(creditRating.index == value){
				return creditRating.description;
			}
		}
		return  "未知类型("+value+")";
	},	
	
});


XFApp.client.prototype.creditRating = {
		successCode : "10000",	
		creditRatingPage : null,
		creditId:null,
		urlItems : {
			initListURL: basePath+"/creditRating/init",		
			changeCreditURL:basePath+"/creditRating/changeCredit"
		},
		
		load : function (){
			
            _this = this;            
			
			var objParams = {				
				customerCode : $("#customerCode").val(),
				customerName : $("#customerName").val(),
				creditLevel : $("#creditLevel").attr("data-id") ,
			}
			
			if(!!_this.creditRatingPage){
				_this.creditRatingPage.page("remote",0,objParams);
			}else{
				_this.creditRatingPage = $("#page").page({
				pageSize : 10,
				remote: {
					url: _this.urlItems.initListURL,
			        params: objParams, 
			        success: function (data, pageIndex) {
			        	if(data.code==_this.successCode){			        		
			        		if(null == data 
									|| null == data.result 
										|| null == data.result.itemList
											|| data.result.itemList.length < 1){
			        			$("#data_grid").html('<tr><td align="center" colspan="11">暂无数据</td></tr>');
			        		}else{
			        			$("#data_grid").html($("#dataTmp").render(data.result.itemList));
			        		}			        		
			        	}else{
			        		window.webDialogUtil.alert("提示",data.msg);			        		   
			        	}
			        },
			        error : function(){			        	
			        	window.webDialogUtil.alert("提示","数据查询失败!");			      	  
			        }
				},
				firstBtnText : '首页',
				lastBtnText : '尾页',
				prevBtnText : '上一页',
				nextBtnText : '下一页',
				showInfo : true,
				showJump : true,
				jumpBtnText : '跳转',
				showPageSizes : true,
				infoFormat : '共{pageCount}页'
			});	
		   }			
		},
		
		initSelect : function (){
			
			$("#creditLevel").bsSuggest({
				idField: "index",  
				keyField: "description",
				effectiveFields :["index","description"],
				effectiveFieldsAlias:{index:"编码",description:"名称"},
		        data: {
		           'value': creditRatingStatus
		        }
		   });
			
		},
		creditPop:function(id,creditLevel){
			this.creditId = id;
			var _this = this;
			webAlert({
				title: '企业信誉等级 ',
	            content:$("#creditSelect_modal").html(),
	            width:250,
	            button:[
	                     {
	                     name:"提交",
	                     callback:function(){
	                    	 _this.changeCredit();
	                     return true;
	                     }
	                     },
	                     {
	                     name:"取消",
	                     callback:function(){
	                     return true;
	                     }
	            }]
			});
			$("#creditSelect_span"+creditLevel+">input").prop("checked",true);
		},
		
		changeCredit : function(id){
			var creditLevel = 0;
			var _this = this;
			$("#creditSelect_div input").each(function(){
				var index = $("#creditSelect_div input").index($(this));
				if($(this).prop("checked")){
					creditLevel = index + 1;
					return;
				}
			});
			var params = {
				creditLevel:creditLevel	,
				id:_this.creditId
			}
			$.ajax({
				url:_this.urlItems.changeCreditURL,
				dataType:'json',
				type:'post',
				data:params,
				success:function(data){
					if(data.code==_this.successCode){		
						window.webDialogUtil.alert("提示","处理成功");	
						_this.load();
					}else{
						window.webDialogUtil.alert("提示",data.msg);	
					}
				}, 
				error : function(){			        	
		        	window.webDialogUtil.alert("提示","调整信誉出现异常!");			      	  
		        }
			});
		},
		getData : function(targetTable,targetId){
			$.ajax({
			    url:basePath+"/log/getList",
			    data:{targetTable:targetTable,targetId:targetId},  
			    type:'post',  
			    cache:false,  
			    dataType:'json',  
			    success:function(data) {
			    	if(true == data.isSuccess){
			    		data = data.result;
			    		var html = '';
			    		html+='<div>'
				    	      +'<table class="table table-bordered" style="margin-bottom:0px;">'
				    	      +'<thead>'
				    	      +'<tr><th width="90px">操作人</th><th width="90px">状态</th><th width="200px">内容</th><th>操作时间</th></tr>'
				    	      +'</thead>'
				    	    +'</table>'
				    	  +'</div>';
			    		html += '<div class="" style="width:600px;height:200px; overflow-y:auto;">';
						html += '<table class="table table-bordered">';
						for(var index in data){
							if(index == 0){
								html += '<tr>'
									+ '<td width="90px">'
									+ data[index].loginName
									+ '</td>'
									+ '<td  width="90px">'
									+ "".getString(data[index].contentInfo)
									+ '</td>'
									+ '<td width="200px" title="'+data[index].content+'">'
									+ "".subString(data[index].content,15)
									+ '</td>'
									+ '<td>'
									+ data[index].actionTime
									+ '</td>'
									+ '</tr>';
							}else{
								html += '<tr>'
									+ '<td>'
									+ data[index].loginName
									+ '</td>'
									+ '<td>'
									+ "".getString(data[index].contentInfo)
									+ '</td>'
									+ '<td title="'+data[index].content+'">'
									+ "".subString(data[index].content,15)
									+ '</td>'
									+ '<td>'
									+ data[index].actionTime
									+ '</td>'
									+ '</tr>';
							}
						}
						html += '</table></div></div>'
						webAlert({
							title: '操作历史 ',
				            content:html
				            });
			    	}
			     },  
			     error : function() {
			    	 webAlert({
			              title:'提示',
			              content:'操作失败!',
			              opacity:.5,
			              bg:"gray",
			              width:310,
			              height:10,
			              button:[
			                      {
			                      name:"关闭",
			                      callback:function(){
			                    	  return true;
			                      }
			                      }]
			              });
			     }  
			});
			
		},
		event : function(){
			_this = this;
			$("#btnSearch").on("click",function(){
				_this.load();
			});
			
			$("#btnReset").on("click",function(){
				$("#customerCode").val("");
				$("#customerName").val("");
				$("#creditLevel").val("");
				$("#creditLevel").attr("data-id","");
			});
		},
			
		
};


$(function(){
	creditRatingCtrl.creditRating.load();
	creditRatingCtrl.creditRating.event();
	creditRatingCtrl.creditRating.initSelect();
});